<template>
	<view class="Single">
		<view @click="handleScan" class="scan-wrapper">
			<image src="../../static/nav/scan-active.png"></image>
		</view>
		<!-- <view class="upload-form" v-if="isUploading">
			正在上传
		</view> -->
		<!-- <book-loading :visible.sync="isUploading"></book-loading> -->
	</view>
</template>

<script>
	import bookLoading from '../../components/bookLoading.vue'
	export default {
		components:{
			bookLoading
		},
		data() {
			return {
				isUploading:false
			};
		},
		methods:{
			async handleScan(){
				// uni.navigateTo({
				// 	url:'../upload/upload'
				// })
				// return
				this.isUploading = true
				let book = {
					"author": "",
					"binding": "",
					"bookCatalog": "",
					"cipTxt": "",
					"edition": "",
					"format": "",
					"genus": "",
					"gist": "",
					"img": "",
					"isbn": "",
					"keyword": "",
					"language": "",
					"page": "",
					"paper": "",
					"price": "",
					"pubDate": "",
					"pubPlace": "",
					"publisher": "",
					"saleVolume": 0,
					"searchTime": 1,
					"series": "",
					"title": "",
					"wordNum": "",
					"yinci": ""
				}
				
				const result = await new Promise((resolve,reject)=>{
					uni.scanCode({
						scanType:['barCode'],
						success: async (res) => {
							try{
								const bookRes = await this.$api.getBookInfo(res.result)
								Object.keys(bookRes).forEach(item=>{
									book[item] = bookRes[item]
								})
								console.log(book);
								resolve(book)
								// const book = {
								// 				"author": "钱乐秋,赵文耘,牛军钰",
								// 				"binding": "平装",
								// 				"bookCatalog": "",
								// 				"cipTxt": "",
								// 				"createTime": 1626530064968,
								// 				"edition": "2",
								// 				"format": "16开",
								// 				"genus": "",
								// 				"gist": "钱乐秋等编著的《软件工程(第2版）》系统地介绍了软件_[程的概念、原理、过程及主要方法，内容上覆盖了ACM和IEEE最新制定的ComputingCurricula中有关软件工程的主要知识点。本书在介绍软件工程的基本概念和基本原理的基础上，重点介绍软件开发方法和技术，包括经典的和常用的方法，如结构化方法、面向数据结构方法和面向对象方法，以及一些软件工程的新技术和新方法，如UMI．2．0、基于构件的开发、敏捷软件开发、Web工程、CMM和CMMI等。此外，本书尽量采用国标、ISO标准及《计算机科学技术百科全书》对专业术语的名称及其语义解释，必要时，术语名称会同时给出其他习惯称谓。《软件工程(第2版）》适合作为高等学校计算机科学与技术学科、软件工程学科各专业的教材，也可作为软件开发人员的参考书。",
								// 				"img": "https://img.maimiaobook.com/cover/LM6D98YV2E.jpg?x-oss-process=style/yuantu",
								// 				"isbn": "9787302328766",
								// 				"keyword": "",
								// 				"language": "",
								// 				"page": "",
								// 				"paper": "",
								// 				"price": "39.50",
								// 				"pubDate": "201308",
								// 				"pubPlace": "",
								// 				"publisher": "清华大学出版社",
								// 				"saleVolume": 0,
								// 				"searchTime": 1,
								// 				"series": "",
								// 				"title": "普通高等教育\"十一五\"国家级规划教材·21世纪大学本科计算机专业系列教材 : 软件工程",
								// 				"wordNum": "",
								// 				"yinci": ""
								// 			}
								
							}catch(err){
								if(err?.errMsg.includes('timeout')){
									uni.showModal({
										showCancel:false,
										title:'错误',
										content:'连接服务器失败，请重试！',
									})
									return
								}
								uni.showModal({
									showCancel:false,
									title:'错误',
									content:`获取书本信息失败，错误原因：${err?.data?.msg || JSON.stringify(err)}\n请手动录入书本信息。`,
								})
								resolve(book)
							}
						},
						fail: (err) => {
							
							if(err.errMsg === 'scanCode:fail cancel'){
								return
							}
							uni.showToast({
								icon:'none',
								mask:true,
								title:`扫码识别失败，失败原因：${err}`
							})
							reject(err)
							
						},
					})
				})
				
				uni.navigateTo({
					url:`../upload/upload?bookInfo=${encodeURIComponent(JSON.stringify(result))}`
				})
				
			}
		},
		mounted() {
			uni.$on('apiShowLoading',()=>{
				this.isUploading = true
			})
			uni.$on('apiHideLoading',()=>{
				this.isUploading = false
			})
		}
	}
</script>

<style lang="scss" scoped>
	.Single{
		position: absolute;
		width: 100%;
		height: 100%;
		// border: 1px solid #007AFF;
		display: flex;
		justify-content: center;
		align-items: center;
		.scan-wrapper{
			// border: 1px solid black;
			width: 300rpx;
			height: 300rpx;
			image{
				width: 100%;
				height: 100%;
			}
		}
	}
</style>
